1

00:00:00,900  -->  00:00:05,380
In the previous video, we already create a
verify config menu.

2

00:00:05,380  -->  00:00:09,200
Now in this video, we are going to create log menu.

3

00:00:09,540  -->  00:00:18,680
Okay let’s just start. Open the visual studio code. First, let’s create a models for Log.

4

00:00:20,020  -->  00:00:23,320
Here I will copy paste from my note.

5

00:00:25,440  -->  00:00:33,800
Here we have some column in the log models.
Target will be used to store the ip address of the router.

6

00:00:33,800  -->  00:00:40,780
Action will be configure or
verify config. And status will be success or failed.

7

00:00:41,180  -->  00:00:46,940
We have time here, this column
will show us when the action is executed.

8

00:00:47,220  -->  00:00:54,720
Finally we have a message here. This column will show us the message if the action is failed.

9

00:00:55,820  -->  00:01:03,120
Because we doing a modification in the models.py
file, so we need to doing a migration.

10

00:01:03,220  -->  00:01:11,840
Open the command prompt, stop the server, and do
a makemigration.

11

00:01:20,140  -->  00:01:23,060
Next let’s do a migrate.

12

00:01:26,020  -->  00:01:29,660
Okay, now let’s run the server again

13

00:01:37,500  -->  00:01:48,720
Now let’s go to views.py file. Here we will
doing a manipulation in the configure &amp; verify_config function.

14

00:01:50,680  -->  00:01:56,540
If we see here, the script will
try to login to the device and send the command.

15

00:01:57,040  -->  00:02:04,000
When try to login to the router, there is
a chance that we got an error, either we use

16

00:02:04,000  -->  00:02:08,500
a wrong IP Address or may be we have a wrong
username and password.

17

00:02:08,860  -->  00:02:11,680
So we need to use try in here.

18

00:02:16,600  -->  00:02:19,720
And then here, we need to use except.

19

00:02:26,300  -->  00:02:35,020
Except block will be executed if we got an error in the try block. Here we will write a script to add data to

20

00:02:35,020  -->  00:02:38,440
the log database that the action is failed.

21

00:02:38,660  -->  00:02:42,300
I will just copy and paste the script from my note.

22

00:02:45,620  -->  00:02:51,980
Here we add a data to the log database. But we need to import Log first.

23

00:03:01,660  -->  00:03:10,280
Here the target will be the ip address of
the router, dev is the variable that we already defined before.

24

00:03:10,800  -->  00:03:17,580
Then the action will be configure, because we doing a manipulation in the configure function.

25

00:03:17,760  -->  00:03:26,360
The status will be error. The time
will be datetime.now, and the message will be e,

26

00:03:26,360  -->  00:03:30,520
which is the variable that save the
exception message.

27

00:03:35,060  -->  00:03:41,400
Here we can see that we use datetime function, so we need to import it first.

28

00:03:55,060  -->  00:04:00,920
Until now, we only add data to log database when the program is failed,

29

00:04:01,540  -->  00:04:10,220
but if the program is success, we don’t add data to log database. Now let’s write a script to add data to

30

00:04:10,220  -->  00:04:16,480
the log database when the program is success.
We can just copy and paste this script.

31

00:04:20,060  -->  00:04:23,120
Let’s change the status to success,

32

00:04:28,100  -->  00:04:33,660
and the message is “no error”, because the program is running successfully.

33

00:04:35,760  -->  00:04:44,740
Okay now we have done in the configure function. Let’s copy and paste this script to the verify_config function.

34

00:05:04,500  -->  00:05:07,960
Let’s change the action to verify config.

35

00:05:15,300  -->  00:05:26,120
Now, let’s create a page to show the log.
Open urls.py file, here let’s create a new url, let’s say log.

36

00:05:28,780  -->  00:05:34,260
And this url will be
redirected to log function inside views.py.

37

00:05:34,500  -->  00:05:38,580
Now let’s create log function in the views.py.

38

00:05:47,360  -->  00:05:51,860
Let’s create a variable to save all of logs from the database.

39

00:05:58,180  -->  00:06:03,020
Next let’s create a context to pass the
variable to the template.

40

00:06:08,720  -->  00:06:12,720
And finally return the template file and the context.

41

00:06:19,760  -->  00:06:27,720
Now let’s create the template file. I already
upload the log.html file in the resources section.

42

00:06:27,720  -->  00:06:32,480
So you can just download it and put
it in the templates directory.

43

00:06:37,680  -->  00:06:48,740
Okay now we have log.html file here. Open
the browser, and navigate to 127.0.0.1:8000/log.

44

00:06:51,500  -->  00:07:00,740
Okay now we have log page here. But we don’t have any log yet, because we don’t do any action for now.

45

00:07:01,040  -->  00:07:07,340
In the next video, we are going to test our
application, so we will get a log in here.

46

00:07:07,340  -->  00:07:10,580
Thankyou for watching and see you on the next
video!
